package vip.zhenzicheng.rabbitmq.routing;

import com.rabbitmq.client.*;
import vip.zhenzicheng.rabbitmq.utils.RabbitConstant;
import vip.zhenzicheng.rabbitmq.utils.RabbitUtils;

import java.io.IOException;

/**
 * @author zhenzicheng
 * @DESCRIPTION:
 * @DATE: 2022/04/25 3:08 PM
 */
public class Baidu {
  public static void main(String[] args) throws IOException {
    Connection conn = RabbitUtils.getConnection();
    final Channel channel = conn.createChannel();
    // 声明队列信息
    channel.queueDeclare(RabbitConstant.QUEUE_BAIDU, false, false, false, null);
    // 指定队列与交换机以及routing key关系 可以绑定多个
    channel.queueBind(RabbitConstant.QUEUE_BAIDU, RabbitConstant.EXCHANGE_WEATHER_ROUTING, "us.cal.lsj.20201127");
    channel.queueBind(RabbitConstant.QUEUE_BAIDU, RabbitConstant.EXCHANGE_WEATHER_ROUTING, "china.hubei.wuhan.20201127");
    channel.queueBind(RabbitConstant.QUEUE_BAIDU, RabbitConstant.EXCHANGE_WEATHER_ROUTING, "us.cal.lsj.20201128");
    channel.queueBind(RabbitConstant.QUEUE_BAIDU, RabbitConstant.EXCHANGE_WEATHER_ROUTING, "china.henan.zhengzhou.20201128");
    channel.basicQos(1);
    channel.basicConsume(RabbitConstant.QUEUE_BAIDU, false, new DefaultConsumer(channel) {
      @Override
      public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
        System.out.println("百度天气收到气象信息：" + new String(body));
        channel.basicAck(envelope.getDeliveryTag(), false);
      }
    });

  }

}
